package com.example.bootcurriculumproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.bootcurriculumproject.entity.User;
import com.example.bootcurriculumproject.mapper.UserMapper;
import com.example.bootcurriculumproject.service.impl.UserService;

import com.example.bootcurriculumproject.util.RedisUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

/**
 * 用户模块
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;
    @JsonFormat
    private int num;
    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @CrossOrigin
    public boolean add(@RequestBody User user){
        userService.insertUser(user);
        return true;
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    @ResponseBody
    @CrossOrigin
    public boolean updateUser(@RequestBody User user){
        userService.updateUser(user);
        return true;
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @DeleteMapping("/delete/{uid}")
    @ResponseBody
    @CrossOrigin
    public boolean delete(@PathVariable String uid){
        userService.deleteUser(uid);
        return true;
    }

    /**
     * 根据username查找用户
     * @param username
     * @return
     */
    @GetMapping("/selectByUserName/{username}")
    @ResponseBody
    @CrossOrigin
    public User selectByUserName(@PathVariable String username){//http://localhost:8080/user/selectByUserName/bbc
        User user=userService.selectByUid(username);
        return user;
    }

    /**
     * 用户登录
     *
     * @return
     */
     //@RequestMapping("/login")
    @GetMapping("/login")//http://localhost:8080/user/login?username=bbc&password=ggdd
    @ResponseBody
    @CrossOrigin
     public String login(@RequestParam("username") String username,@RequestParam("password") String password){
        User user = userService.selectByUid(username);
        if (user!=null){
            //UID保存到redis中
            if(password.equals(user.getPassword())){
                redisUtil.set("nowUid",user.getUid(),0);
                //修改登录状态--》改为1
                 userService.updateStatus(user.getUid(),1);
                return JSON.toJSONString("登录成功");
            }else{
                //用户名密码错误
                return JSON.toJSONString("账号密码错误");
            }
        }else {
            //该用户未注册--》自动跳转到注册页面
            return JSON.toJSONString("找不到用户请注册");
        }
     }

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    @CrossOrigin//跨域
    public boolean register(@RequestBody User user){
        user.setStatus(0);
        userService.insertUser(user);
        return true;
    }
    /**
     * 用户登出
     *
     * @return
     */
    @GetMapping("/loginOut")
    @ResponseBody
    @CrossOrigin
    public boolean loginOut(){//http://localhost:8080/user/loginOut
        String uid = String.valueOf(redisUtil.get("nowUid"));
        //修改登录状态为0
         userService.updateStatus(uid,0);
        //清除redis
        redisUtil.delRedis("nowUid");
        return true;
    }
    /**
     * 根据uid获取登录状态
     * @param uid
     * @return
     */
    @GetMapping("/getStatus/{uid}")
    @ResponseBody
    @CrossOrigin
    public int getStatus(@PathVariable String uid){
        return userService.getStatus(uid);
    }

    /**
     * 根据uid修改用户状态
     * @param uid
     * @return
     */
    @PutMapping("/updateStatus")
    @ResponseBody
    @CrossOrigin
    public boolean updateStatus(@RequestParam("uid") String uid,@RequestParam("status") int status){
        userService.updateStatus(uid,status);
        return true;
    }

    //返回用户Uid
    @GetMapping("/getUid")
    @ResponseBody
    @CrossOrigin
    public String getUid(){//http://localhost:8080/user/getUid
        return String.valueOf(redisUtil.get("nowUid"));
    }
    //通过uid获取用户--uid在登录时自动保存到redis，直接通过访问方法就行
    @GetMapping("/getUserByUid")
    @ResponseBody
    @CrossOrigin
    public User getUserByUid(){
        String uid = String.valueOf(redisUtil.get("nowUid"));
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUid,uid);
        return userMapper.selectOne(queryWrapper);
    }
    //修改用户头像
    @PutMapping("/updateImage")
    @ResponseBody
    @CrossOrigin
    public boolean updateImage(@PathVariable String image){
        User user = userService.selectByUid(String.valueOf(redisUtil.get("nowUid")));
        user.setImage(image);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUid,String.valueOf(redisUtil.get("nowUid")));
        userMapper.update(user,queryWrapper);
        return true;
    }
    /**
     * 微信用户登录
     *传入状态码1，以及获取到达微信用户个人信息进行登录
     * @return
     */
    @GetMapping("/WXLogin")
    @ResponseBody
    @CrossOrigin
    public String WXLogin(){
     redisUtil.set("nowUid","7788",0);
     userService.selectByUid("7788");
     return "success";
    }
}
